package com.w7vm.menuweb.utility;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import com.w7vm.menuweb.entity.AcessoWeb;

	@FacesConverter(value = "restaurantePickListConverter") 
	public class RestaurantePickListConverter  implements Converter { 

		public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) { 
			Object ret = null; 
			
			if (arg1 instanceof PickList) { 
				Object dualList = ((PickList) arg1).getValue(); 
				DualListModel dl = (DualListModel) dualList; 
				
				for (Iterator iterator = dl.getSource().iterator(); iterator .hasNext();) 
				{ 
					Object o = iterator.next(); 
					String id = (new StringBuilder()).append( ((AcessoWeb) o).getRestauranteSedeWeb().getIdRestauranteSedeWeb()).toString(); 
					if (arg2.equals(id)) { ret = o; break; } } if (ret == null) { 
						for (Iterator iterator1 = dl.getTarget().iterator(); iterator1 .hasNext();) { 
							Object o = iterator1.next(); 
							String id = (new StringBuilder()).append( ((AcessoWeb) o).getRestauranteSedeWeb().getIdRestauranteSedeWeb()).toString(); 
							if (arg2.equals(id)) { 
								ret = o; break; 
								} 
							} 
						} 
					} 
			return ret; 
		} 
		
		
		@Override
		public String getAsString(FacesContext context, UIComponent component, Object value) {
			int retorno = 0;
			if (!(value == null)) {
				AcessoWeb acesso = new AcessoWeb();
				acesso = (AcessoWeb) value;
				retorno = acesso.getRestauranteSedeWeb().getIdRestauranteSedeWeb();
			}
			return Integer.toString(retorno);
		}
}
